<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class order extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        parent::checkLogin();

    }

//订单信息
    function orderList()
    {

        $array = array("aa.invalid" => 0);
        $result = $this->db
            ->select('aa.id,order_id,state,pay_way,pay_time,total_fee,create_time,agent_id,role_id,bb.father_id,cc.dcrp,cc.title,aa.type')
            ->from('Record_Orders aa')
            ->join('Record_Agent bb', 'aa.agent_id = bb.agentid', 'left')
            ->join('Record_Roles cc', 'aa.role_id = cc.rid and aa.type = cc.cv', 'left')
            ->where($array)
            ->get()
            ->result_array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('orderlist.html');
        $this->load->view('footer.html');
    }

    function orderListDownload()
    {
        $array = array("aa.invalid" => 0);
        $result = $this->db
            ->select('aa.id,order_id,state,pay_way,pay_time,total_fee,create_time,agent_id,role_id,bb.father_id,cc.dcrp')
            ->from('Record_Orders aa')
            ->join('Record_Agent bb', 'aa.agent_id = bb.agentid', 'left')
            ->join('Record_Roles cc', 'aa.role_id = cc.rid and aa.type = cc.cv', 'left')
            ->where($array)
            ->get()
            ->result_array();

        $head = array('#', '订单号',  '状态', '支付方式', '支付时间','支付金额' ,'生成时间','代理ID', '套餐编号','上级代理ID', '标题');
        array_unshift($result, $head);

        $fileName=date('Y-m-d',time()).'-订单列表';
        $this->output($result,$fileName);
    }

    function closeOrder()
    {
        $id = $this->uri->segment(3);

        $array = array("aa.invalid" => 0, 'aa.id' => $id);

        $result = $this->db
            ->select('aa.id,order_id,state,pay_way,pay_time,total_fee,create_time,agent_id,role_id,bb.father_id,cc.dcrp')
            ->from('Record_Orders aa')
            ->join('Record_Agent bb', 'aa.agent_id = bb.agentid', 'left')
            ->join('Record_Roles cc', 'aa.role_id = cc.rid and aa.type = cc.cv', 'left')
            ->where($array)
            ->get()
            ->result_array();

        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('closeorder.html');
        $this->load->view('footer.html');
    }


    function doCloseOrder()
    {
        $id = $this->input->post('id');
        $array = array("invalid" => 0, 'id' => $id);

        //  'time'=>time()
        $ret = $this->db->where($array)->get('Record_Orders')->result_array();
        if (!$ret) {
            $this->alert_message(0, '订单已经关闭', site_url('order/orderList'));
            exit;
        }
        unset($array['invalid']);
        $data = array("invalid" => 1);
        $ret = $this->db->where($array)->update('Record_Orders', $data);
        if ($ret) {
            $this->alert_message(1, '关闭成功', site_url('order/orderList'));
        } else {
            $this->alert_message(0, '关闭失败', site_url('order/orderList'));
        }

    }
}